package com.tnxst.phexamination.adapter.follow;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.dto.TbFirstFollowDTO;
import com.tnxst.phexamination.dto.TbFollowDTO;
import com.tnxst.phexamination.fragment.follow.FirstPhthisisFragment;
import com.tnxst.phexamination.fragment.follow.PhthisisFollowFragment;
import com.tnxst.phexamination.utils.StringUtils;

import java.util.List;

public class TbFollowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private FragmentManager fragmentManager;

    private List<TbFollowDTO> tbFollowDTOS;

    private PhthisisFollowFragment fragment;



    public TbFollowAdapter(List<TbFollowDTO> tbFollowDTOS, FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.tbFollowDTOS = tbFollowDTOS;
    }


    public void setTbFollowDTOS(List<TbFollowDTO> tbFollowDTOS) {
        this.tbFollowDTOS = tbFollowDTOS;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view =  LayoutInflater.from(context).inflate(R.layout.tb_follow_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holders = (ViewHolder) holder;
        TbFollowDTO tbFollowDTO = tbFollowDTOS.get(position);

        holders.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(tbFollowDTO);
            }
        });

        if (StringUtils.isNotNull(tbFollowDTO.getPersonName()))
            holders.patientName.setText(tbFollowDTO.getPersonName());

        if (StringUtils.isNotNull(tbFollowDTO.getDoctorName()))
            holders.doctorName.setText(tbFollowDTO.getDoctorName());

        if (StringUtils.isNotNull(tbFollowDTO.getFollowUpDate()))
            holders.followDate.setText(tbFollowDTO.getFollowUpDate());

        if (StringUtils.isNotNull(tbFollowDTO.getNextFollowUpDate()))
            holders.nextFollowDate.setText(tbFollowDTO.getNextFollowUpDate());

        if (StringUtils.isNotNull(tbFollowDTO.getWayUp())){
            String followType = tbFollowDTO.getWayUp();
            switch (followType){
                case "1":
                    holders.followType.setText("门诊");
                    break;
                case "2":
                    holders.followType.setText("家庭");
                    break;
            }
        }


        if (StringUtils.isNotNull(tbFollowDTO.getSupervisor())){
            String patientType = tbFollowDTO.getSupervisor();
            switch (patientType){
                case "1":
                    holders.patientType.setText("医生");
                    break;
                case "2":
                    holders.patientType.setText("家属");
                    break;
                case "4":
                    holders.patientType.setText("自服药");
                    break;
                case "8":
                    holders.patientType.setText("其他");
                    break;
            }
        }


    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View view;

        TextView patientName;

        TextView doctorName;

        TextView followDate;

        TextView nextFollowDate;

        TextView followType;

        TextView patientType;


        public ViewHolder(View view) {
            super(view);
            this.view = view;
            this.patientName = view.findViewById(R.id.textView166);
            this.doctorName = view.findViewById(R.id.textView168);
            this.followDate = view.findViewById(R.id.textView276);
            this.nextFollowDate = view.findViewById(R.id.textView305);
            this.followType = view.findViewById(R.id.textView171);
            this.patientType = view.findViewById(R.id.textView173);
        }
    }

    @Override
    public int getItemCount() {
        return tbFollowDTOS.size();
    }


    private void showFragment(TbFollowDTO tbFollowDTO) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
        fragment = new PhthisisFollowFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("tbFollowDTO", tbFollowDTO);
        fragment.setArguments(bundle);
        ft.replace(android.R.id.content, fragment);
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

}
